package com.water.guider.highlight

import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.RectF

/**
 * 矩形的高亮区域.
 */
class RectHighlight : IHighlight {

    override var width = 0
    override var height = 0
    var padding = 0

    constructor() {}

    @JvmOverloads
    constructor(width: Int, height: Int, padding: Int = 0) {
        this.width = width
        this.height = height
        this.padding = padding
        init()
    }

    override fun init() {
        width = width + padding * 2
        height = height + padding * 2
    }

    override var ringWidth: Int = 0

    override fun draw(canvas: Canvas, paint: Paint, x: Int, y: Int) {
        val left = x - width / 2
        val top = y - height / 2
        val right = x + width / 2
        val bottom = y + height / 2
        val rectF = RectF(left.toFloat(), top.toFloat(), right.toFloat(), bottom.toFloat())
        canvas.drawRect(rectF, paint)
    }

    override fun draw(canvas: Canvas, paint: Paint, x: Int, y: Int, value: Int) {
        var left = x - value
        var top = y - value
        var right = x + value
        var bottom = y + value
        if (left < x - width / 2) {
            left = x - width / 2
        }
        if (top < y - height / 2) {
            top = y - height / 2
        }
        if (right > x + width / 2) {
            right = x + width / 2
        }
        if (bottom > y + height / 2) {
            bottom = y + height / 2
        }
        val rectF = RectF(left.toFloat(), top.toFloat(), right.toFloat(), bottom.toFloat())
        canvas.drawRect(rectF, paint)
    }

    override val max: Int
        get() = width.coerceAtLeast(height)
}